﻿using DevExpress.XtraGrid.Views.BandedGrid;
using DLL;
using NursingFormSystem.Model;
using NursingFormSystem.PICCNursingWashpipe;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.PICCNursing
{
    public partial class HistoryPICC : Form
    {
        public NursingFormSystem.NurFormsService.NursingForm formService = new NursingFormSystem.NurFormsService.NursingForm();
        Tb_User LoginUser;//当前的登录信息
        PatientInfo Patientinfo;
        public HistoryPICC(Tb_User loginUser, PatientInfo patientInfo)
        {
            InitializeComponent();
            this.LoginUser = loginUser;
            this.Patientinfo = patientInfo;
            BindControl();
            BindInfo();
            SetBandGridView();
        }

        private void BindControl()
        {
            this.StatDate.DateTime = DateTime.Now.AddDays(-30);
            this.EndDate.DateTime = DateTime.Now;
        }

        #region  查询
        private void btnSearch_Click(object sender, EventArgs e)
        {
            BindInfo();
        }
        #endregion

        #region  新增
        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddPICC addForm = new AddPICC(LoginUser, Patientinfo, string.Empty);
            addForm.ShowDialog();
            if (addForm.DialogResult == DialogResult.OK)
            {
                BindInfo();
            }
        }
        #endregion

        #region 修改
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (bandedGridView1.RowCount > 0)
            {
                string Id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                AddPICC addForm = new AddPICC(LoginUser, Patientinfo, Id);
                addForm.ShowDialog();
                if (addForm.DialogResult == DialogResult.OK)
                {
                    BindInfo();
                }
            }
        }
        #endregion

        #region  打印
        private void btnPrint_Click(object sender, EventArgs e)
        {
            PICCNursingWashpipePrints f = new PICCNursingWashpipePrints(Patientinfo, StatDate.DateTime, EndDate.DateTime);
            f.Show();
        }
        #endregion

        #region  删除
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (bandedGridView1.RowCount <= 0) return;
            if (MessageBox.Show("确定要删除数据吗？", "删除前确认", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                string id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                var ret2 = formService.DeleteNursingFormRecord(id);
                MessageBox.Show(ret2.M);
                if (ret2.Code == 1)
                {
                    string dateStart = this.StatDate.Text;
                    string dateEnd = this.EndDate.Text;
                    BindInfo();
                }
            }
        }
        #endregion

        #region LoadData显示
        private void BindInfo()
        {
            string patientid = Patientinfo.PATIENT_ID.Trim();//住院号
            int VisitId = Patientinfo.VISIT_ID;//住院次数
            int FormId = (int)FormType.PICCWashPipeNursing;//18001  //表单ID
            var ret = formService.GetNursingFormRecordByDate(patientid, VisitId, FormId, this.StatDate.DateTime, this.EndDate.DateTime);
            DataTable dt = new DataTable();
            dt.Columns.Add("RiQi", typeof(string));
            dt.Columns.Add("WaiLu", typeof(string));
            dt.Columns.Add("ShangTunWei", typeof(string));
            dt.Columns.Add("GuanDao", typeof(string));
            dt.Columns.Add("GengHuanGanSuMao", typeof(string));
            dt.Columns.Add("GengHuanFuTie", typeof(string));
            dt.Columns.Add("ChongGuanYeTi", typeof(string));
            dt.Columns.Add("FengGuan", typeof(string));
            dt.Columns.Add("ChuanCiZhengChang", typeof(string));
            dt.Columns.Add("ChuanCiShenXue", typeof(string));
            dt.Columns.Add("ChuanCiShenYe", typeof(string));
            dt.Columns.Add("ChuanCiHongZhon", typeof(string));
            dt.Columns.Add("ChuanCiQiTa", typeof(string));
            dt.Columns.Add("HuLiCuoShi", typeof(string));
            dt.Columns.Add("QianZi", typeof(string));
            dt.Columns.Add("Id", typeof(string));
            if (ret.Code == 1)
            {
                string theTrue = "√";
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                foreach (NursingFormRecord record in list)
                {
                    var NursingFormData = record.NursingFormData;
                    PICCWashPipe fd = new PICCWashPipe();
                    fd = XmlUtil.XMLToObject<PICCWashPipe>(NursingFormData, Encoding.UTF8);
                    DataRow newRow = dt.NewRow();
                    newRow["RiQi"] = Convert.ToDateTime(record.AssessmentDate).ToString("yyyy-MM-dd");
                    newRow["WaiLu"] = fd.WaiLuChangDu;
                    newRow["ShangTunWei"] = fd.ShangTunWei;
                    newRow["GuanDao"] = fd.GuanLuShiFouChangTong;
                    newRow["GengHuanGanSuMao"] = fd.GengHuanGanSuMao;
                    newRow["GengHuanFuTie"] = fd.GengHuanFuTie;
                    newRow["ChongGuanYeTi"] = fd.ChongGuanYeTi.Equals("1") ? theTrue : string.Empty;
                    newRow["FengGuan"] = fd.FengGuan.Equals("1") ? theTrue : string.Empty;
                    if (!string.IsNullOrEmpty(fd.ChuangCiBuWeiQingKuang))
                    {
                        string ysgj_yf = fd.ChuangCiBuWeiQingKuang.IndexOf("#") < 0 ? "0#0#0#0#0" : fd.ChuangCiBuWeiQingKuang;
                        string[] ysgj_yf_arrary = ysgj_yf.Split('#');
                        if (ysgj_yf_arrary.Length >= 5)
                        {
                            newRow["ChuanCiZhengChang"] = ysgj_yf_arrary[0].Equals("1") ? theTrue : string.Empty;
                            newRow["ChuanCiShenXue"] = ysgj_yf_arrary[1].Equals("1") ? theTrue : string.Empty;
                            newRow["ChuanCiShenYe"] = ysgj_yf_arrary[2].Equals("1") ? theTrue : string.Empty;
                            newRow["ChuanCiHongZhon"] = ysgj_yf_arrary[3].Equals("1") ? theTrue : string.Empty;
                            newRow["ChuanCiQiTa"] = ysgj_yf_arrary[4].Equals("1") ? (!string.IsNullOrEmpty(fd.ChuangCiBuWeiQingKuangWenBenKuang) ? fd.ChuangCiBuWeiQingKuangWenBenKuang : theTrue) : string.Empty;
                        }
                    }
                    newRow["HuLiCuoShi"] = fd.HuLiCuoShi;
                    newRow["QianZi"] = fd.QianMing;
                    newRow["Id"] = record.Id;
                    dt.Rows.Add(newRow);
                }
            }
            gridControl1.DataSource = dt;
        }
        #endregion

        #region GridView设置
        //GridView设置
        private void SetBandGridView()
        {
            foreach (GridBand gridBand in this.bandedGridView1.Bands)//设置居中
            {
                foreach (GridBand gridBandSecond in gridBand.Children)
                {
                    gridBandSecond.OptionsBand.FixedWidth = true;
                    gridBandSecond.AppearanceHeader.Options.UseTextOptions = true;
                    gridBandSecond.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                    gridBandSecond.AppearanceHeader.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
                }
                gridBand.OptionsBand.FixedWidth = true;
                gridBand.AppearanceHeader.Options.UseTextOptions = true;
                gridBand.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                gridBand.AppearanceHeader.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
            }
        }
        #endregion

        #region 双击事件
        private void bandedGridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (bandedGridView1.RowCount <= 0) return;
            if (e.Clicks == 2)
            {
                string Id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                AddPICC add = new AddPICC(LoginUser, Patientinfo, Id);
                add.ShowDialog();
                if (add.DialogResult == DialogResult.OK)
                {
                    BindInfo();
                }
            }
        }
        #endregion
    }
}